package cn.tedu.egarticle0811.base.exception;

import cn.tedu.egarticle0811.base.response.JsonResult;
import cn.tedu.egarticle0811.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandleRuntimeException(RuntimeException ex) {
        log.debug("运行时异常信息："+ex.getMessage());
        String data = ex.getMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler(MethodValidationException.class)
    public JsonResult doHandleMethodValidationException(MethodValidationException ex) {
        log.debug("方法参数验证异常信息："+ex.getMessage());
        String data = ex.getMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex) {
        log.debug("方法参数验证异常信息："+ex.getMessage());
        String data = ex.getMessage();
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
}
